/*
 * @Description: 订单校验规则页面
 * @Version: 1.0
 * @Autor: c-jack.qian
 * @Date: 2020-10-21 17:30:52
 * @LastEditors: c-jack.qian
 * @LastEditTime: 2021-04-25 16:47:28
 */

import { renderSort } from '@src/utils/utilsFun';
import { Button, DataSet, notification, Table } from 'choerodon-ui/pro/lib';
import { ButtonColor, FuncType } from 'choerodon-ui/pro/lib/button/enum';
import { TableColumnTooltip } from 'choerodon-ui/pro/lib/table/enum';
import { Buttons } from 'choerodon-ui/pro/lib/table/Table';
import React, { useCallback, useState } from 'react';
import intl from 'utils/intl';
import { connect } from 'dva';
import { ColumnProps } from 'choerodon-ui/pro/lib/table/Column';
import { useDataSetEvent } from 'hzero-front/lib/utils/hooks';
// import { reqCustomerDateSet } from '@src/stores/minOrderDtlDS';
// import { DataSetProps } from 'choerodon-ui/pro/lib/data-set/DataSet';


notification.config({
  duration: 3,
  placement: 'bottomRight',
});
interface propstype {
  dispatch: any;
  tableDS: DataSet;
  parentDS: DataSet;
  areaDS: DataSet;
}

const ClassPage: React.FC<propstype> = ({ tableDS, parentDS, areaDS }) => {
  const [addFlag, setAddFlag] = useState(true);
  const [, updateComponent] = useState({});
  const forceUpdateComponent = useCallback(() => updateComponent({}), []);


  // 新建行
  const handleCreate = () => {
    tableDS.create({});
    const currentRecord = tableDS.current;
    currentRecord?.set('valueCaculationRule', '1');
    currentRecord?.set('orderCycle', '1');
    currentRecord?.set('quantityCaculationRule', 1);
  };

  
  // 删除选择行
  const handleDelete = () => {
    const seletedRecord = tableDS.selected;
    if (seletedRecord.length === 0) {
      notification.warning({
        placement: 'bottomRight',
        message: intl.get(`dji.otc.om.view.reminder.warning`).d('提示'),
        description: intl.get(`dji.otc.om.view.reminder.selectDeleteData`).d('请勾选删除的数据'),
      });
      return;
    }
    tableDS.delete(seletedRecord);
  };

  // 更新行列表列
  useDataSetEvent(parentDS, 'update', ({ name, dataSet }) => {
    console.log(dataSet.current.get('amountRuleType'));
    
    if (name === 'amountRuleType' && dataSet.current.get('amountRuleType')) {
      forceUpdateComponent()
    }
  });



  useDataSetEvent(areaDS, 'update', ({ name, dataSet }) => {
    if (name === 'check') {
      const allData = dataSet.toData();
      let runFlag: boolean = true;
      for (const iterator of allData) {
        if (iterator.check) {
          runFlag = false;
          break;
        }
      }
      setAddFlag(runFlag);
    }
  });


  const ColumnsFn = () => {
    let agentColumn: Object[] = [];
    if (parentDS.current?.get('amountRuleType') === 'WHITE_LIST_RULES') {
      agentColumn = [
        {
          name: 'customerCode',
          align: 'left',
          tooltip: TableColumnTooltip.always,
          editor: true,
          width: 300,
        },
      ];
      tableDS.getField('customerCode')?.set('required', true);
    } else {
      tableDS.getField('customerCode')?.set('required', false);
    }
    console.log(agentColumn);
    
    return [
      {
        header: intl.get(`dji.otc.om.view.field.serialNumber`).d('序号'),
        width: 50,
        lock: true,
        renderer: ({ record }) => renderSort(record, tableDS),
      },
      ...agentColumn,
      {
        name: 'minOrderValue',
        align: 'left',
        tooltip: TableColumnTooltip.always,
        editor: true,
      },
      {
        name: 'valueCaculationRule',
        align: 'left',
        tooltip: TableColumnTooltip.always,
        editor: true,
      },
      { name: 'minOrderQuantity', editor: true, align: 'left', tooltip: TableColumnTooltip.always },
      {
        name: 'maxOrderQuantity',
        align: 'left',
        tooltip: TableColumnTooltip.always,
        editor: true,
      },
      {
        name: 'orderCycle',
        align: 'left',
        tooltip: TableColumnTooltip.always,
        width: 170,
        editor: true,
      },
      {
        name: 'quantityCaculationRule',
        editor: true,
        align: 'left',
        tooltip: TableColumnTooltip.always,
      },
    ] as ColumnProps[];
  };


  const buttons: Buttons[] = [
    <Button
      icon="add"
      key="create"
      color={ButtonColor.default}
      funcType={FuncType.raised}
      onClick={() => handleCreate()}
      disabled={addFlag}
    >
      {intl.get(`dji.otc.om.view.button.create`).d('新建')}
    </Button>,
    <Button
      icon="delete"
      key="delete"
      color={ButtonColor.default}
      funcType={FuncType.raised}
      onClick={() => handleDelete()}
      disabled={addFlag}
    >
      {intl.get(`dji.otc.om.view.button.delete`).d('删除')}
    </Button>,
  ];
  return (
    <Table
      style={{ margin: '20px 0' }}
      dataSet={tableDS}
      border
      queryFieldsLimit={3}
      columns={ColumnsFn()}
      buttons={buttons}
      header={intl.get('dji.otc.om.view.menu.ruleLine').d('校验规则行')}
    />
  );
};

export default connect()(ClassPage);
